/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PeerView.java
 *
 * Created on Mar 1, 2013, 1:07:00 PM
 */
package edu.ncsu.csc573.project1.gui;

import edu.ncsu.csc573.project1.peer.Peer;
import edu.ncsu.csc573.project1.util.Constants;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 *
 * @author user
 */
public class PeerView extends javax.swing.JFrame implements WindowListener, ActionListener  {

    AddPanel addPanel;
    LookupPanel lookupPanel;
    ListPanel listPanel;
    Peer peer;
    JMenu connectMenu;
    JMenu serverMenu;
    JMenuBar menuBar;
    JMenuItem serverMenuItem, connectMenuItem;
    /** Creates new form PeerView */
    public PeerView(String[] args) {
        initComponents();
        Runnable splashThread = new Runnable() {
            public void run() {
                MySplashScreen.active = true;
                MySplashScreen.currentMessage = "Loading RFCs...";
                MySplashScreen demo = new MySplashScreen();
            }
        };
        new Thread(splashThread).start();

        //this.remove(uploadScrollPane);
        //this.add(uploadScrollPane, BorderLayout.SOUTH);

        int clientType = 0;
        if (args.length != 0) {
            clientType = Integer.parseInt(args[0]);
        }
        peer = new Peer(uploadEditorPane);
        MySplashScreen.active = false;
        String s = (String) JOptionPane.showInputDialog(this,
                                                        "Set Server Address to : ",
                                                        "Set Server Address",
                                                        JOptionPane.PLAIN_MESSAGE,
                                                        null,
                                                        null,
                                                        Constants.kHostName);
        
        Constants.kHostName = s;
        peer.connectToServer();
        peer.initializePeerWithType(clientType);
        this.addWindowListener(this);
        this.tabber.add("Add", addPanel = new AddPanel(peer));
        this.tabber.add("Lookup", lookupPanel = new LookupPanel(peer));
        this.tabber.add("List", listPanel = new ListPanel(peer));
        
        menuBar = new JMenuBar();
        serverMenu = new JMenu("Server");
        menuBar.add(serverMenu);
                
        serverMenuItem = new JMenuItem("Change the Server Adress");
        serverMenu.add(serverMenuItem);
        serverMenuItem.addActionListener(this);

        this.setJMenuBar(menuBar); 
        this.pack();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabber = new javax.swing.JTabbedPane();
        uploadScrollPane = new javax.swing.JScrollPane();
        uploadEditorPane = new javax.swing.JEditorPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));
        getContentPane().add(tabber);

        uploadEditorPane.setEditable(false);
        uploadEditorPane.setMaximumSize(new java.awt.Dimension(100, 16));
        uploadScrollPane.setViewportView(uploadEditorPane);

        getContentPane().add(uploadScrollPane);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        final String myArgs[] = args;
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PeerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PeerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PeerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PeerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display` the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PeerView(myArgs).setVisible(true);
                /*try {
                    Image im  = ImageIO.read(new File("resources/rfcicon.png"));
                
                    Application application = Application.getApplication();
                    application.setDockIconImage(im);
                } catch (Exception e) {
                    e.printStackTrace();
                }*/
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabber;
    private javax.swing.JEditorPane uploadEditorPane;
    private javax.swing.JScrollPane uploadScrollPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public void windowOpened(WindowEvent we) {
       
    }

    @Override
    public void windowClosing(WindowEvent we) {
        peer.closeConnection();
    }

    @Override
    public void windowClosed(WindowEvent we) {
       
    }

    @Override
    public void windowIconified(WindowEvent we) {
        
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
     
    }

    @Override
    public void windowActivated(WindowEvent we) {  
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
      
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
       String s = (String) JOptionPane.showInputDialog(this,
                                                        "Change Server Address to : ",
                                                        "Change Server Address",
                                                        JOptionPane.PLAIN_MESSAGE,
                                                        null,
                                                        null,
                                                        Constants.kHostName);

        Constants.kHostName = s;
        peer.connectToServer();
        if (peer.isConnected()) {
            addPanel.setRegisteredStatus("Yes");
        } else {
            addPanel.setRegisteredStatus("No");
        }
    }
    
    /*class ServerDialog extends JDialog implements ActionListener {
        
        private JButton okButton = new JButton("Ok");
        private JTextField addressField = new JTextField(20);
        
        private ServerDialog() {
            super(new JFrame(), "Change Server Address", true);
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.add(addressField, BorderLayout.CENTER);
            panel.add(okButton, BorderLayout.SOUTH);
            this.add(panel);
            this.pack();            
        }
        
        public void actionPerformed(ActionEvent ae) {
            Constants.kHostName = addressField.getText();
            peer.connectToServer();
            System.out.println("Hostname changed to : " + Constants.kHostName);
        }
    }*/
}
